#include <iostream>

using namespace std;

const int N = 1e5 + 10;
int p[N], h, t, n;
int main()
{
	cin >> n;
	while(n--)
	{
		int op, x;
		cin >> op;
		if(op == 1)
		{
			cin >> x;
			p[++t] = x;
		}
		else if(op == 2)
		{
			if(h == t)
			    cout << "ERR_CANNOT_POP" << endl;
			else
			    h++;
		}
		else if(op == 3)
		{
			if(h == t)
			    cout << "ERR_CANNOT_QUERY" << endl;
		    else
			    cout << p[h+1] << endl;
	    }
		else
		    cout << t - h << endl;
	}
	return 0;
}
